package com.greenant.store.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.greenant.store.R;
import com.greenant.store.bean.UserIdentification;
import com.greenant.store.utils.Const;
import com.lsarah.utils.SystemBarTintManager;

public class MyAlipayActivity extends BaseActivity {
	private TextView mTvName;
	private TextView mTvIdNum;
	private TextView mTvAlipayAccount;
	
	private UserIdentification mUserIdentification;
	
	private final int RequestCodeChangeAccount = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.my_alipay_activity,
				getResources().getColor(R.color.firebrick));
		
		mTvName = (TextView) findViewById(R.id.my_alipay_name);
		mTvIdNum = (TextView) findViewById(R.id.my_alipay_id_num);
		mTvAlipayAccount = (TextView) findViewById(R.id.my_alipay_alipay_account);
		
		mUserIdentification = Const.mUserIdentification;
		refreshInfo();
	}
	
	private void refreshInfo() {
		if(mUserIdentification != null) {
			String name = mUserIdentification.getRealName();
			if(!TextUtils.isEmpty(name)) {
				String subName = name.substring(1);
				mTvName.setText("*" + subName);
			}
			
			String idNum = mUserIdentification.getIDNum();
			if(!TextUtils.isEmpty(idNum)) {
				if(idNum.length() > 2) {
					String subIdNum = idNum.substring(idNum.length() - 2);
					for(int i=0; i<idNum.length()-2; i++) {
						subIdNum = "*" + subIdNum;
					}
					mTvIdNum.setText(subIdNum);
				} else {
					mTvIdNum.setText(idNum);
				}
			}
			
			String account = mUserIdentification.getAccount();
			if(!TextUtils.isEmpty(account)) {
				if(account.length() > 2) {
					String subAccount = account.substring(account.length() - 2);
					for(int i=0; i<account.length()-2; i++) {
						subAccount = "*" + subAccount;
					}
					mTvAlipayAccount.setText(subAccount);
				} else {
					mTvAlipayAccount.setText(account);
				}
			}
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == RequestCodeChangeAccount) {
			if(resultCode == RESULT_OK) {
				refreshInfo();
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			this.setResult(RESULT_OK);
			this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	public void goBack(View view) {
		this.setResult(RESULT_OK);
		finish();
	}
	
	public void changeAccount(View view) {
		Intent intent = new Intent(this, MyAlipayChangeAccountActivity.class);
		startActivityForResult(intent, RequestCodeChangeAccount);
	}
}
